﻿using System;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;

namespace ProjetoComperio.BusinessLogicLayer
{
    public class AcademicPersonBLL : ComperioDataObject
    {

        public void Create(AcademicPerson academicPerson)
        {
            if (String.IsNullOrEmpty(academicPerson.WebPage))
                academicPerson.WebPage = "http://www.faetec.rj.gov.br/ist-rio";
            DbContext.People.Add(academicPerson);
            DbContext.SaveChanges();
        }

        public void Delete(AcademicPerson academicPerson)
        {
            DbContext.People.Remove(academicPerson);
            DbContext.SaveChanges();
        }

        public AcademicPerson RetrieveAcademicPersonByID(Int32 personID)
        {
            return DbContext.People.OfType<AcademicPerson>().Where(academicPerson => academicPerson.PersonID == personID).FirstOrDefault();
        }

        public IQueryable<AcademicPerson> RetrieveAcademicPersons()
        {
            return DbContext.People.OfType<AcademicPerson>();
        }

        public AcademicPerson RetrieveAcademicPersonByEnrolment(String enrolment)
        {
            return this.RetrieveAcademicPersons().Where(academicPerson => academicPerson.Enrollment.Equals(enrolment)).FirstOrDefault();

        }

        /// <summary>
        /// This method returns an academic person by your userName
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public AcademicPerson RetrieveAcademicPersonByUserName(String userName)
        {
            //return DbContext.People.OfType<AcademicPerson>().Where(academicPerson => academicPerson.User.FirstOrDefault().UserName.Equals(userName)).FirstOrDefault();
            return new AcademicPerson();
        }

        public Boolean IsValidEnrolment(String enrolment)
        {
            return !String.IsNullOrEmpty(enrolment) && RetrieveAcademicPersonByEnrolment(enrolment) == null;
        }

        /// <summary>
        /// TODO: Como reportar erros?
        /// </summary>
        /// <param name="contribution"></param>
        /// <param name="academicPerson"></param>
        public Boolean RegisterAcademicPersonInContribution(int contributionID, int academicPersonID)
        {

            var _contribution = DbContext.Contributions.FirstOrDefault(c => c.ContributionID == contributionID);
            var _academicPerson = DbContext.People.OfType<AcademicPerson>().FirstOrDefault(ap => ap.PersonID == academicPersonID);

            if (_contribution == null || _academicPerson == null)
                return false;

            _academicPerson.Contributions.Add(_contribution);
            DbContext.SaveChanges();
            return true;
        }

        /// <summary>
        /// Retrieve all academic people related to a contribution
        /// </summary>
        /// <param name="contributionID"></param>
        /// <returns></returns>
        public IQueryable<AcademicPerson> RetrieveAcademicPeopleInContribution(int contributionID)
        {//TODO: Testar mockando o banco
            return DbContext.Contributions.FirstOrDefault(c => c.ContributionID == contributionID).AcademicPeople.AsQueryable();
        }
    }
}
